Tack.Game = new Class({
	initialize: function (__decompiling) {
		this.title = 'Untitled';
		this.author = 'Anonymous';
		this.description = '';

		this.scenes = new Hash();

		if (!__decompiling) {
			this.startingScene = new Tack.Scene('Start');
			this.setScene(this.startingScene);
		}
	},

	changeSceneReferences: function (oldName, newName) {
		var s = this.scenes.get(oldName);
		if (!s) return;

		this.scenes.remove(oldName);
		this.scenes.set(newName, s);

		this.scenes.each(function (scene) {
			scene.changeSceneReferences(oldName, newName);
		});
	},

	compile: function () {
		var scenes = [];
		this.scenes.each(function (scene) {
			scenes.push(scene.compile());
		});

		return {
			title: this.title,
			author: this.author,
			description: this.description,
			startingScene: this.startingScene.name,
			scenes: scenes
		};
	},

	setScene: function (scene) {
		if (!scene instanceof Tack.Scene)
			throw '[scene] must be an instance of Tack.Scene.';

		scene.setGame(this);
		this.scenes.set(scene.name, scene);
	}
});

Tack.Game.decompile = function (obj) {
	var game = new Tack.Game(true);
	game.title = obj.title;
	game.author = obj.author;
	game.description = obj.description;

	for (var i = 0; i < obj.scenes.length; i++) {
		game.setScene(Tack.Scene.decompile(obj.scenes[i]));
	}

	game.startingScene = game.scenes.get(obj.startingScene);
	
	return game;
};
